package com.hooda.service;

import com.hooda.mapper.EmployeeMapper;
import com.hooda.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author zhangzp
 * @version 1.0, 2021/9/30
 */
@Service
@CacheConfig(cacheNames = {"emp"})
public class EmployeeService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Cacheable(key = "#id", condition = "#id > 0", unless = "#result == null")
    public Employee getEmployeeById(Integer id) {
        return employeeMapper.getById(id);
    }

    @CachePut(key = "#entity.id")
    public Employee updateEmployee(Employee entity) {
        employeeMapper.update(entity);
        return entity;
    }

    @CacheEvict(key = "#id", beforeInvocation = true)
    public void deleteEmployee(Integer id) {
        employeeMapper.delete(id);
    }
}
